﻿CREATE PROCEDURE [acms].[Post_Delete]
	@Id int,
	@VersionId int,
	@ConcurencyId rowversion
AS
	Update [Posts]
	Set Deleted = 1
	From [Posts] a inner join
		[PostVersions] b on a.Id = b.PostId and b.Id = @VersionId
	Where a.Id = @Id
		and b.ConcurencyId = @ConcurencyId
		and Deleted = 0
		
	if @@RowCount <> 1 begin
		if Exists (Select null From Post_View Where Id = @id and VersionId = @VersionId)
			return -100
		else
			return -200
	end
	
	/*
	declare @ParentId int
	--update siblings sequence field for Pendings
	Select @ParentId = ParentId FROM [PostState] where Id = @Id and State = 1
	
	update [PostState]
		set Sequence = b.RowNumber
	from [PostState] a inner join
		(Select Id, Row_Number() Over (Order By Sequence) - 1 as RowNumber
		From [PostState]
	Where ParentId = @ParentId
		and Deleted = 0
		) b on a.Id = b.Id

	--update siblings sequence field for Live
	*/
RETURN 0;